/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.springvision.yuanfeng.device.service;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.editor.language.json.converter.util.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.springvision.yuanfeng.device.entity.Device;
import com.springvision.yuanfeng.device.mapper.DeviceMapper;
import com.springvision.yuanfeng.device.web.SerialPortController;

/**
 * 设备Service
 * @author ycb
 * @version 2018-07-20
 */
@Service
@Transactional(readOnly = true)
public class DeviceService extends CrudService<DeviceMapper, Device> implements Serializable {

	public Device get(String id) {
		return super.get(id);
	}

	public List<Device> findList(Device device) {
		return super.findList(device);
	}

	public Page<Device> findPage(Page<Device> page, Device device) {
		return super.findPage(page, device);
	}

	@Transactional(readOnly = false)
	public void save(Device device) {
		super.save(device);
	}

	@Transactional(readOnly = false)
	public void delete(Device device) {
		super.delete(device);
	}

	@Transactional(readOnly = false)
	public void saveOffline(Device device) {
		super.save(device);
		SerialPortController._DEVICE_CACHE.put(device.getCode(), device);
	}

	@Transactional(readOnly = false)
	public void saveOnline(Device device) {
		super.save(device);
	}

	@Transactional(readOnly = false)
	public void deleteOffline(Device device) {
		super.delete(device);
		SerialPortController._DEVICE_CACHE.remove(device.getCode());
	}

	/**
	 * 此处主键只考虑code并不考虑场站信息，该方法只用作于离线版本
	 * 
	 * @return
	 * @author: Elvin Zhao - 279152260@qq.com 
	 * @date: Created on 2018年7月24日 上午10:31:13
	 */
	public Map<String, Device> findCodeMap() {
		Map<String, Device> devMap = new HashMap<String, Device>(4);
		List<Device> devList = mapper.findAllList(new Device());
		if (CollectionUtils.isNotEmpty(devList)) {
			for (Device dev : devList) {
				devMap.put(dev.getCode(), dev);
			}
		}
		return devMap;

	}

	/**
	 * 获取场站的设备id 和设备name name是null的情况获取code
	 * 
	 * @param stationId
	 * @return
	 * @author: Elvin Zhao - 279152260@qq.com 
	 * @date: Created on 2018年8月3日 上午10:35:32
	 */
	public List<Device> findDeviceCode(String stationId) {
		return mapper.findDeviceCode(stationId);
	}

	public Device findDeviceCodeByStaDev(String stationId, String devCode) {
		return mapper.findDeviceCodeByStaDev(stationId, devCode);
	}
}